﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using Com.Web.coffinegurunaru.AppLogic.Page;
using Com.Library.DB.User;
using Com.Library.Page;
using Com.Framework;
using Com.Framework.Data;
using Com.Framework.Enc;

namespace Com.Web.coffinegurunaru.manager.user
{
	public partial class userMethod : ManagerAjaxPage
	{
		public string[] GetList(int PageNo, byte PageSize, string IsLocalUser, byte nGroupType, string strDateJoin_start, string strDateJoin_end, byte SearchType, string SearchValue)
		{
			string UserID_search = string.Empty;
			string UserName_search = string.Empty;
			byte? GroupType = nGroupType == 0 ? null : (byte?)nGroupType;
			DateTime? DateJoin_start = null;
			DateTime? DateJoin_end = null;
			DateJoin_start = strDateJoin_start == string.Empty ? DateJoin_start : Convert.ToDateTime(strDateJoin_start);
			DateJoin_end = strDateJoin_start == string.Empty ? DateJoin_end : Convert.ToDateTime(strDateJoin_end);


			if (DateJoin_start < DateJoin_end)
			{
				DateTime? tmp = DateJoin_start;
				DateJoin_start = DateJoin_end;
				DateJoin_end = tmp;
			}

			if (SearchType == 0)
			{
				UserID_search = string.Empty;
				UserName_search = string.Empty;
			}

			if (SearchType == 1)
				UserID_search = SearchValue.Trim();
			if (SearchType == 2)
				UserName_search = SearchValue.Trim();


			ListData<UserEntity, OutputEntity> result = UserModel.GetList(0, PageNo, PageSize, IsLocalUser, UserID_search, UserName_search, GroupType, DateJoin_start, DateJoin_end);

			string[] retValue = new string[3];
			Dictionary<string, object> properties = new Dictionary<string, object>();
			properties.Add("UserList", result.Record);
			retValue[0] = ViewManager<ManagerPage>.Render("/manager/user/userList.ascx", properties);
			retValue[1] = result.Output.CntTotal.ToString();
			retValue[2] = result.Output.CntRow.ToString();
			return retValue;
		}

		public string[] GetListByRemove(int PageNo, byte PageSize, string IsLocalUser, byte nGroupType, string strDateJoin_start, string strDateJoin_end, byte SearchType, string SearchValue)
		{
			string UserID_search = string.Empty;
			string UserName_search = string.Empty;
			byte? GroupType = nGroupType == 0 ? null : (byte?)nGroupType;
			DateTime? DateJoin_start = null;
			DateTime? DateJoin_end = null;
			DateJoin_start = strDateJoin_start == string.Empty ? DateJoin_start : Convert.ToDateTime(strDateJoin_start);
			DateJoin_end = strDateJoin_start == string.Empty ? DateJoin_end : Convert.ToDateTime(strDateJoin_end);

			if (SearchType == 0)
			{
				UserID_search = string.Empty;
				UserName_search = string.Empty;
			}

			if (SearchType == 1)
				UserID_search = SearchValue.Trim();
			if (SearchType == 2)
				UserName_search = SearchValue.Trim();


			ListData<UserEntity, OutputEntity> result = UserModel.GetList(1, PageNo, PageSize, IsLocalUser, UserID_search, UserName_search, GroupType, DateJoin_start, DateJoin_end);

			string[] retValue = new string[3];
			Dictionary<string, object> properties = new Dictionary<string, object>();
			properties.Add("UserList", result.Record);
			retValue[0] = ViewManager<ManagerPage>.Render("/manager/user/removeuserlist.ascx", properties);
			retValue[1] = result.Output.CntTotal.ToString();
			retValue[2] = result.Output.CntRow.ToString();
			return retValue;
		}

		public void Remove(int UserNo)
		{
			UserModel.Remove(UserNo);
		}

		public void Restore(int UserNo)
		{
			UserModel.Restore(UserNo);
		}

		public string GetInfo(int UserNo)
		{
			UserEntity info = UserModel.UserInfo(UserNo);
			string retValue = string.Empty;
			Dictionary<string, object> properties = new Dictionary<string, object>();
			properties.Add("UserInfo", info);
			retValue = ViewManager<ManagerPage>.Render("/manager/user/userinfo.ascx", properties);
			return retValue;
		}

		public void Modify(int UserNo, string IsLocalUser,
			string UserID, string UserPWD,
			string UserName, string Email1, string Email2, string IsEmail, byte Question, string Answer,
			string PostNo1, string PostNo2, string Address1, string Address2,
			string HomePhone1, string HomePhone2, string HomePhone3,
			string MobilePhone1, string MobilePhone2, string MobilePhone3, string IsSMS,
			string Birthday_Year, string Birthday_Month, string Birthday_Day, string IsLunar,
			byte JoinMotiveType, byte CodeSex)
		{
			UserModifyArguments arg = new UserModifyArguments();
			arg.n4UserNo = UserNo;
			arg.IsLocalUser = IsLocalUser;
			arg.strUserID = UserID;
			if (UserPWD != string.Empty)
				arg.strMD5PWD = Crypt.MD5HashCrypt(UserPWD);
			arg.strUserName = UserName;
			arg.strEmail = Email1 + "@" + Email2;
			arg.IsEmail = IsEmail;
			arg.n1Question = Question;
			arg.strAnswer = Answer;
			if ( PostNo1 != "" && PostNo2 != "" )
				arg.strPostNo = PostNo1 + "-" + PostNo2;
			arg.strAddress1 = Address1;
			arg.strAddress2 = Address2;
			if (HomePhone1 != "" && HomePhone2 != "" && HomePhone3 != "")
			arg.strHomePhone = HomePhone1 + "-" + HomePhone2 + "-" + HomePhone3;
			arg.strMobilePhone = MobilePhone1 + "-" + MobilePhone2 + "-" + MobilePhone3;
			arg.IsSMS = IsSMS;
			arg.strBirthday = Birthday_Year + "-" + Birthday_Month + "-" + Birthday_Day;
			arg.IsLunar = IsLunar;
			arg.n1JoinMotiveType = JoinMotiveType;
			arg.CodeSex = CodeSex;
			UserModel.Modify(arg);
		}
	}
}

